package com.sq.imp.Calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;

public class CalculatorWindow extends JFrame {
    private final JTextField expressionField;
    private final JTextArea resultArea;
    private ExpressionEvaluator evaluator;

    public CalculatorWindow(String expression, Point mouseLocation) {
        setTitle("自动计算器");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置窗口位置为鼠标位置
        setLocation(mouseLocation.x, mouseLocation.y);
        // 初始化表达式评估器
        evaluator = new ExpressionEvaluator(expression);
        // 创建面板
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 表达式栏
        JLabel expressionLabel = new JLabel("表达式:");
        expressionField = new JTextField(expression);
        expressionField.addActionListener(e -> calculate());

        // 结果栏
        JLabel resultLabelTitle = new JLabel("结果:");
        resultArea = new JTextArea("");
        resultArea.setEditable(false); // 设置为不可编辑
        resultArea.setLineWrap(true); // 开启自动换行
        resultArea.setWrapStyleWord(true); // 按单词换行

        JLabel saveSetting = new JLabel("保留小数位:");
        JTextField saveSettingField = new JTextField("5");

        JScrollPane scrollPane = new JScrollPane(resultArea);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // 按需显示垂直滚动条
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // 不显示水平滚动条[^13^][^16^]

        // 添加组件到面板
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 添加边距

        // 添加表达式标签
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(expressionLabel, gbc);

        // 添加表达式输入框
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0; // 占据多余空间
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(expressionField, gbc);

        // 添加结果标签
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(resultLabelTitle, gbc);

        // 添加结果显示
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0; // 占据多余空间
        gbc.weighty = 1.0; // 占据多余高度
        gbc.fill = GridBagConstraints.BOTH; // 填充水平和垂直方向
        panel.add(scrollPane, gbc);


        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(saveSetting, gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.weightx = 1; // 占据多余空间
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(saveSettingField, gbc);
        // 添加面板到窗口
        add(panel);

        // 自动计算一次
        calculate();

        // 添加焦点监听器
        addWindowFocusListener(new WindowFocusListener() {
            @Override
            public void windowGainedFocus(WindowEvent e) {
                // 窗口获得焦点时不做任何操作
            }

            @Override
            public void windowLostFocus(WindowEvent e) {
                // 窗口失去焦点时关闭窗口
//                dispose();
            }
        });

        saveSettingField.addActionListener(e -> {
            try{
                ExpressionEvaluator.save = Integer.parseInt(saveSettingField.getText());
            }catch (Exception ignore) {
                JOptionPane.showMessageDialog(this, "不是一个合法数字", "提示", JOptionPane.INFORMATION_MESSAGE);
                ignore.printStackTrace();
            }
        });
    }

    private void calculate() {
        try {
            String expression = expressionField.getText();
            if (expression == null || expression.trim().isEmpty()) {
                resultArea.setText("0");
            }else {
                evaluator = new ExpressionEvaluator(expression);
                BigDecimal result = evaluator.evaluate().stripTrailingZeros();
                resultArea.setText(result.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultArea.setText("计算错误");
        }
    }
}